package hikari.script;

import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.utils.FileContentReplacer;
import hikari.utils.Utils;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

/**
 * @author sad
 */
@WiringComponent
public class ProjectCreator {

    @Wire
    private Utils utils;
    @Wire
    private FileContentReplacer fileContentReplacer;
    
    public void createProject(String folder) {
        String path = utils.combinePath(utils.getWorkDirectory(), "templates", "projectTemplate");
        try {
            FileUtils.copyDirectory(new File(path), new File(folder));
            fixIncludePath(utils.combinePath(folder, "cpp", "project", "project.vcxproj"));
        } catch (IOException ex) {
            utils.logError(ex);
        }
    }

    protected void fixIncludePath(String projectFilePath) {
        String irrlichtIncludes = utils.combinePath(utils.getWorkDirectory(), "include");
        String[] chunks = irrlichtIncludes.split("\\\\");
        boolean first = true;
        StringBuilder sb=new StringBuilder();
        for (String chunk : chunks) {
            if(first==false){
                sb.append("\\\\");
            }
            sb.append(chunk);
            first = false;
        }
        irrlichtIncludes=sb.toString();
        String includes = irrlichtIncludes + ";\\$(SolutionDir)\\\\project;\\$(IncludePath)";
        fileContentReplacer.replaceTextInFileWithoutComments("IncludePath", "/IncludePath", projectFilePath, includes);
    }
}
